package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.InviteCheckBean;
import com.yiwei.ydd.api.bean.SetInviteBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.event.RegisterSuccessEvent;
import com.yiwei.ydd.util.Dialog;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class InviteFriendRegisterActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    TextView btnWechat;
    @BindView(R.id.edit_invite)
    EditText editInvite;
    @BindView(R.id.btn_change_phone)
    TextView btnChangePhone;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_skip)
    TextView btnSkip;
    //    @BindView(R.id.btn_agreement)
//    TextView btnAgreement;

    @BindView(R.id.btn_clear)
    ImageView btnClear;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    private Unbinder unbiner;
    private String invite_code = "";
    private String phone_input = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_invite);
        initWebViewBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        editInvite.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                btnClear.setVisibility(View.VISIBLE);
                if (UI.toString(editInvite).length() == 11) {
//                    if (phone_input.equals(UI.toString(editInvite))) {
//                        return;
//                    }
                    phone_input = UI.toString(editInvite);
                    getInviteCheck(phone_input);
                } else {
//                    phone_input = "";
                }
            }
        });
    }

    @OnClick({R.id.btn_clear, R.id.btn_back, R.id.btn_submit, R.id.btn_skip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_clear:
                invite_code = "";
                editInvite.setText("");
                break;

            case R.id.btn_back:
            case R.id.btn_skip:
                showSkipDialog();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editInvite))) {
                    ToastUtil.makeText(this, "邀请码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(invite_code)) {
                    getSetInvite(UI.toString(editInvite));
                } else {
                    getSetInvite(invite_code);
                }

                break;
//            case R.id.btn_agreement:
//                getPagesDetail();
//                new PagesDetailDialog(this).setKeyword(Const.PageDetailType.APP_SERVICE).show();
//                break;

        }
    }

    private void showSkipDialog() {
        Dialog.showDefaultDialog(this, "", "填写邀请码，好友将获得佣金，您确定要跳过吗？", "跳过", "继续填写", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {

            }

            @Override
            public void cancel() {
                Util.startActivity(InviteFriendRegisterActivity.this, CreateGestureActivity.class);
                EventBus.getDefault().post(new RegisterSuccessEvent());
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        showSkipDialog();
    }

    public void getInviteCheck(String keyword) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        InviteCheckBean bean = new InviteCheckBean();
        bean.keyword = keyword;

        Api.api_service.getInviteCheck(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (TextUtils.isEmpty(result.datas.invite_code)) {
                        ToastUtil.makeText(this, "该用户未生成邀请码");
                        return;
                    }
                    invite_code = result.datas.invite_code;
                    editInvite.setText(UI.toString(editInvite) + "(好友邀请码:" + invite_code + ")");
//                    sureRefereeDilaog.setInviteData(UI.toString(editName));
//                    sureRefereeDilaog.show();
                });
    }


    public void getSetInvite(String code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        ((BaseActivity) this).loadingDialog.show();
        SetInviteBean bean = new SetInviteBean();
        bean.invite_code = code;
        Api.api_service.getSetInvite(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        ((BaseActivity) this).loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Util.startActivity(InviteFriendRegisterActivity.this, CreateGestureActivity.class);
                    ToastUtil.makeText(this, "设置成功");
                    EventBus.getDefault().post(new RegisterSuccessEvent());
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }
}
